package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by william_sun on 2016-07-15.
 * Description:查看本地取的图片列表
 */
public class LocalPhotoPagerAdapter extends PagerAdapter {
    private ArrayList<String> Urls;
    private Context context;
    private ArrayList<View> images;
    private int currentPage;
    private ImageLoader imageLoader;

    public LocalPhotoPagerAdapter(Context context, ArrayList<String> urls) {
        this.context = context;
        this.Urls = urls;
        imageLoader = ImageLoader.getInstance();
        inits();
    }

    private void inits() {
        images = new ArrayList<>();
        for (String url : Urls) {
            View picView = View.inflate(context, R.layout.view_photo, null);
            images.add(picView);
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(images.get(position));
        final PhotoView photoView = (PhotoView) images.get(position).findViewById(R.id.iv_photo);
        String path = Urls.get(position);
        if (path.startsWith("http")) {
            imageLoader.displayImage(path, photoView, ImageOptionUtils.getInstance().getSimplePicOptions());
        } else {
            File cameraFile = new File(path);
            Bitmap mBitmap = BitmapFactory.decodeFile(cameraFile.getAbsolutePath());
            photoView.setImageBitmap(mBitmap);
        }

        return images.get(position);
    }
}
